package com.gy.paidao.main.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.paidao.R;
import com.gy.paidao.main.bean.ActivityBean;
import com.gy.paidao.util.ListViewItemListener;
import com.jky.struct2.bitmap.FinalBitmap;
import com.jky.struct2.bitmap.FinalBitmapManager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

/**
 * @author GY
 * @date 16/3/2
 * @Description:
 */
public class MainListAdapter extends BaseAdapter {

    private Context context;
    private ListViewItemListener listener;
    private List<ActivityBean> list;
    private FinalBitmap finalBitmap;

    public MainListAdapter(Context context,List<ActivityBean> list){
        this.context = context;
        this.list = list;
        finalBitmap = FinalBitmapManager.getFinalBitmapManager(context)
                .getFinalBitmap(FinalBitmapManager.IMG_BIG);
    }

    public void setListener(ListViewItemListener listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_main_list,null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.adapter_main_list_tv_title);
            holder.tvContent = (TextView) convertView.findViewById(R.id.adapter_main_list_tv_content);
            holder.tvLoc = (TextView) convertView.findViewById(R.id.adapter_main_list_tv_loc);
            holder.tvFire = (TextView) convertView.findViewById(R.id.adapter_main_list_tv_fire);
            holder.tvComment = (TextView) convertView.findViewById(R.id.adapter_main_list_tv_comment);
            holder.imgHead = (ImageView) convertView.findViewById(R.id.adapter_main_list_img_head);
            holder.imgBg = (ImageView) convertView.findViewById(R.id.adapter_main_list_img_bg);
            holder.llFire = (LinearLayout) convertView.findViewById(R.id.adapter_main_list_ll_fire);
            holder.llComment = (LinearLayout) convertView.findViewById(R.id.adapter_main_list_ll_comment);
            holder.llDelete = (LinearLayout) convertView.findViewById(R.id.adapter_main_list_ll_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(list.get(position).title+"");
        holder.tvContent.setText(list.get(position).description+"");
        holder.tvLoc.setText(list.get(position).position+"");
        holder.tvFire.setText(list.get(position).heatValue+"");
        holder.tvComment.setText(list.get(position).commentNum + "");

        String pic = "";
        if("image".equals(list.get(position).mediaType)){
            pic = list.get(position).mediaPaths;
        } else if("video".equals(list.get(position).mediaType)){
            pic = list.get(position).thumbnailUrl;
        }

        if(!TextUtils.isEmpty(list.get(position).headPortrait)){
            ImageLoader.getInstance().displayImage(
                    list.get(position).headPortrait, holder.imgHead, new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String s, View view) {

                        }

                        @Override
                        public void onLoadingFailed(String s, View view, FailReason failReason) {
                            holder.imgHead.setImageResource(R.mipmap.ic_test_head);
                        }

                        @Override
                        public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                        }

                        @Override
                        public void onLoadingCancelled(String s, View view) {
                            holder.imgHead.setImageResource(R.mipmap.ic_test_head);
                        }
                    });
        } else {
            holder.imgHead.setImageResource(R.mipmap.ic_test_head);
        }

        if(!TextUtils.isEmpty(pic)){
//            ImageLoader.getInstance().displayImage(pic,
//                    holder.imgBg, new ImageLoadingListener() {
//                        @Override
//                        public void onLoadingStarted(String s, View view) {
//
//                        }
//
//                        @Override
//                        public void onLoadingFailed(String s, View view, FailReason failReason) {
//                            holder.imgBg.setImageResource(R.mipmap.ic_main_list_test_bg);
//                        }
//
//                        @Override
//                        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//
//                        }
//
//                        @Override
//                        public void onLoadingCancelled(String s, View view) {
//
//                        }
//                    });
            finalBitmap.display(holder.imgBg,pic,R.mipmap.ic_main_list_test_bg);
        } else {
            holder.imgBg.setImageResource(R.mipmap.ic_main_list_test_bg);
        }

        holder.llFire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    if(list.get(position).heated == 0){
                        listener.doPassActionListener(null,1,position,null);
                    }
                }
            }
        });
        holder.llComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.doPassActionListener(null,2,position,null);
                }
            }
        });
        holder.llDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.doPassActionListener(null,3,position,null);
                }
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView tvTitle,tvContent,tvLoc,tvFire,tvComment;
        ImageView imgHead,imgBg;
        LinearLayout llFire,llComment,llDelete;
    }
}
